package by.dezhits.liltweets.fragment;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import by.dezhits.liltweets.R;
import by.dezhits.liltweets.adapter.DraftCursorAdapter;
import by.dezhits.liltweets.api.TwitterAPI;
import by.dezhits.liltweets.content.contract.PostContract;
import by.dezhits.liltweets.db.DBHelper;
import by.dezhits.liltweets.db.DBHelper.PostFlags;
import by.dezhits.liltweets.http.HttpRequestBuilder;
import by.dezhits.liltweets.service.HttpService;

public class DraftFragment extends ListFragment {

	private DraftCursorAdapter draftCursorAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = super.onCreateView(inflater, container, savedInstanceState);
		setRetainInstance(true);

		draftCursorAdapter = new DraftCursorAdapter(this.getActivity(), null);
		this.setListAdapter(draftCursorAdapter);
		getLoaderManager().initLoader(0, null, new LoaderCallbacks<Cursor>() {

			@Override
			public Loader<Cursor> onCreateLoader(int paramInt,
					Bundle paramBundle) {
				return new CursorLoader(DraftFragment.this.getActivity(),
						PostContract.PostColumns.CONTENT_URI, null, null, null,
						null);
			}

			@Override
			public void onLoadFinished(Loader<Cursor> paramLoader, Cursor paramD) {
				draftCursorAdapter.swapCursor(paramD);

			}

			@Override
			public void onLoaderReset(Loader<Cursor> paramLoader) {
				draftCursorAdapter.swapCursor(null);
			}
		});
		return view;
	}

	@Override
	public void onStart() {
		super.onStart();
		this.setEmptyText(getString(R.string.no_records));

	}

	@Override
	public void onListItemClick(final ListView l, final View v,
			final int position, long id) {
		AlertDialog ad = new AlertDialog.Builder(getActivity()).create();
		ad.setCancelable(true);
		ad.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.send),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Cursor cursor = (Cursor) getListView()
								.getItemAtPosition(position);
						sendRecord(cursor.getString(cursor
								.getColumnIndex(PostContract.PostColumns.TEXT)));
					}
				});
		ad.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.delete),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Cursor cursor = (Cursor) l.getItemAtPosition(position);
						final String text = cursor.getString(cursor
								.getColumnIndex(PostContract.PostColumns.TEXT));
						new Thread(new Runnable() {
							@Override
							public void run() {

								getActivity().getContentResolver().delete(
										PostContract.PostColumns.CONTENT_URI,
										PostContract.PostColumns.TEXT + "='"
												+ text + "'", null);
							}
						}).start();
					}
				});
		ad.show();
	}

	public void sendRecord(String status) {
		Intent intent = new Intent(getActivity(), HttpService.class);
		HttpRequestBuilder httpRequestBuilder = new HttpRequestBuilder();
		httpRequestBuilder.setType(HttpRequestBuilder.Type.POST);
		httpRequestBuilder
				.setTwitterRequest(HttpRequestBuilder.TwitterRequest.STATUSES_UPDATE);
		httpRequestBuilder.setUrl(TwitterAPI.URL.TWITTER_STATUSES_UPDATE_URL);
		httpRequestBuilder.appendQueryParameter(
				TwitterAPI.QueryParametr.STATUS, status);
		intent.putExtra(HttpService.HTTP_REQUEST_BUILDER, httpRequestBuilder);
		getActivity().startService(intent);
	}

}
